// pages/signIn/signInRecord/signInRecord.js
var app = getApp()
var util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    course_num: '',
    class_num: '',
    date: '',
    loading: false
  },

  //各状态列表
  StateRequest: function(class_num, date, course_num, state) {
    var header = app.globalData.header
    var that = this
    wx.request({
      url: 'http://139.199.192.171:8080/nanitao/Record/List.action',
      method: 'GET',
      header: header,
      data: {
        class_num: class_num,
        date: date,
        course_num: course_num,
        state: state
      },
      success: function(res) {
        switch (state) {
          case '缺席':
            {
              var length = res.data.length
              that.setData({
                unsignList: res.data,
                unsignIn: length
              })
              console.log(res.data)
              break
            }

          case '出席':
            {
              var length = res.data.length
              that.setData({
                signList: res.data,
                signIn: length
              })
              break
            }

          case '请假':
            {
              var length = res.data.length
              that.setData({
                askList: res.data,
                ask: length
              })
              break
            }

          case '迟到':
            {
              var length = res.data.length
              that.setData({
                lateList: res.data,
                late: length,
                loading: false
              })
              break
            }
        }

        that.setData({
          sum: that.data.unsignIn + that.data.signIn + that.data.ask + that.data.late
        })
      }
    })
  },

  //手动更改签到状态按钮
  BtnRequest: function(stu_num, course_num, state, class_num, date) {
    var header = app.globalData.header
    var time = util.formatDate(new Date())
    var that = this
    wx.request({
      url: 'http://139.199.192.171:8080/nanitao/attence_record/manager.action',
      data: {
        att_stu_num: stu_num,
        att_course_num: course_num,
        att_state: state,
        att_time: time
      },
      method: 'POST',
      header: header,
      success: function(res) {
        that.StateRequest(class_num, date, course_num, '缺席')
        that.StateRequest(class_num, date, course_num, state)
      }
    })
  },

  cancelRequest: function(att_id, class_num, date, course_num, state) {
    var that = this
    var header = app.globalData.header
    wx.request({
      url: 'http://139.199.192.171:8080/nanitao/attence_record/delete.action',
      header: header,
      method: 'POST',
      data: {
        att_id: att_id
      },
      success: function(res) {
        that.StateRequest(class_num, date, course_num, '缺席')
        that.StateRequest(class_num, date, course_num, state)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var date = options.date
    var course_num = options.course_num
    if (!app.globalData.classnum) {
      var class_num = options.class_num
    } else {
      var class_num = app.globalData.classnum
    }
    this.setData({
      course_num: course_num,
      loading: true,
      class_num: class_num,
      date: date
    })
    var that = this
    //各签到状态统计数
    // this.CountRequest(class_num, date, course_num)
    //缺席状态列表
    this.StateRequest(class_num, date, course_num, '缺席')
    //出席状态列表
    this.StateRequest(class_num, date, course_num, '出席')
    //请假状态列表
    this.StateRequest(class_num, date, course_num, '请假')
    //迟到状态列表
    this.StateRequest(class_num, date, course_num, '迟到')
  },

  lateBtn: function(e) {
    var that = this
    //获取点击的对应序列号
    var id = e.currentTarget.dataset.index
    var unsignList = that.data.unsignList
    //根据序列号获取对应的学号信息
    var stu_num = unsignList[id].stu_num
    var course_num = that.data.course_num
    var class_num = that.data.class_num
    var date = that.data.date
    var nowDate = util.formatDate(new Date())
    var nowDate = nowDate.match(/(\d*-)*\d*/)[0]
    if (date == nowDate) {
      that.BtnRequest(stu_num, course_num, '迟到', class_num, date)
    } else {
      wx.showModal({
        title: '提示',
        content: '不可对非当天课程执行此操作',
        showCancel: false
      })
    }
  },

  askBtn: function(e) {
    var that = this
    //获取点击的对应序列号
    var id = e.currentTarget.dataset.index
    var unsignList = that.data.unsignList
    //根据序列号获取对应的学号信息
    var stu_num = unsignList[id].stu_num
    var course_num = that.data.course_num
    var class_num = that.data.class_num
    var date = that.data.date
    var nowDate = util.formatDate(new Date())
    var nowDate = nowDate.match(/(\d*-)*\d*/)[0]
    if (date == nowDate) {
      that.BtnRequest(stu_num, course_num, '请假', class_num, date)
    } else {
      wx.showModal({
        title: '提示',
        content: '不可对非当天课程执行此操作',
        showCancel: false
      })
    }
  },

  remedyBtn: function(e) {
    var that = this
    //获取点击的对应序列号
    var id = e.currentTarget.dataset.index
    var unsignList = that.data.unsignList
    //根据序列号获取对应的学号信息
    var stu_num = unsignList[id].stu_num
    var course_num = that.data.course_num
    var class_num = that.data.class_num
    var date = that.data.date
    var nowDate = util.formatDate(new Date())
    var nowDate = nowDate.match(/(\d*-)*\d*/)[0]
    if (date == nowDate) {
      that.BtnRequest(stu_num, course_num, '出席', class_num, date)
    } else {
      wx.showModal({
        title: '提示',
        content: '不可对非当天课程执行此操作',
        showCancel: false
      })
    }
  },

  sign_cancelBtn: function(e) {
    var that = this
    var id = e.currentTarget.dataset.index
    var signList = that.data.signList
    var att_id = signList[id].att_id
    var course_num = that.data.course_num
    var class_num = that.data.class_num
    var date = that.data.date
    var nowDate = util.formatDate(new Date())
    var nowDate = nowDate.match(/(\d*-)*\d*/)[0]
    if (date == nowDate) {
      that.cancelRequest(att_id, class_num, date, course_num, '出席')
    } else {
      wx.showModal({
        title: '提示',
        content: '不可对非当天课程执行此操作',
        showCancel: false
      })
    }
  },

  ask_cancelBtn: function(e) {
    var that = this
    var id = e.currentTarget.dataset.index
    var askList = that.data.askList
    var att_id = askList[id].att_id
    var course_num = that.data.course_num
    var class_num = that.data.class_num
    var date = that.data.date
    var nowDate = util.formatDate(new Date())
    var nowDate = nowDate.match(/(\d*-)*\d*/)[0]
    if (date == nowDate) {
      that.cancelRequest(att_id, class_num, date, course_num, '请假')
    } else {
      wx.showModal({
        title: '提示',
        content: '不可对非当天课程执行此操作',
        showCancel: false
      })
    }
  },

  late_cancelBtn: function(e) {
    var that = this
    var id = e.currentTarget.dataset.index
    var lateList = that.data.lateList
    var att_id = lateList[id].att_id
    var course_num = that.data.course_num
    var class_num = that.data.class_num
    var date = that.data.date
    var nowDate = util.formatDate(new Date())
    var nowDate = nowDate.match(/(\d*-)*\d*/)[0]
    if (date == nowDate) {
      that.cancelRequest(att_id, class_num, date, course_num, '迟到')
    } else {
      wx.showModal({
        title: '提示',
        content: '不可对非当天课程执行此操作',
        showCancel: false
      })
    }
  },

  refresh: function (class_num, date, course_num){
    this.StateRequest(class_num, date, course_num, '缺席')
    //出席状态列表
    this.StateRequest(class_num, date, course_num, '出席')
    //请假状态列表
    this.StateRequest(class_num, date, course_num, '请假')
    //迟到状态列表
    this.StateRequest(class_num, date, course_num, '迟到')
  },

  onPullDownRefresh:function() {
    var that = this
    var course_num = that.data.course_num
    var class_num = that.data.class_num
    var date = that.data.date
    that.refresh(class_num, date, course_num)
    wx.stopPullDownRefresh({
      success: function () {
        console.log("end")
      }
    })
  }
})